(function($) {

	$.fn.extend({
		'disableEmpty' : function() {
			$('input[name][value=""],input[name]:not([value])', $(this)).each(function() {
				var $this = $(this);
				if ($this.val() == '') {
					$this.prop('disabled', true);
				}
			});
		},
		'enableDisable' : function() {
			$('input:disabled', $(this)).each(function() {
				var $this = $(this);
				$this.prop('disabled', false);
			});
		}
	});

	$.ajaxform = {
		'options' : {
			tabContainerId : 'tabContainer'
		},
		'buildQueryParam' : function(serializeArray) {
			var queryParam = {};
			$.each(serializeArray, function() {
				if (queryParam[this.name] == undefined) {
					queryParam[this.name] = [ this.value ];
				} else {
					queryParam[this.name].push(this.value);
				}
			});
			return queryParam;
		},
		'disableEmpty' : function(frm) {
			$('input[name][value=""],input[name]:not([value])', $(frm)).each(function() {
				var $this = $(this);
				if ($this.val() == '') {
					$this.prop('disabled', true);
				}
			});
		},
		'validate' : function() {
			var isValid = $(this).form('validate');
			if (isValid) {
				$(this).disableEmpty();
			}
			return isValid;
		},
		'reset' : function() {
			var ajaxform = $.dialog.getForm(this);
			ajaxform.form('reset');
		},
		'callbackAction' : function(action) {
			switch (action.operate) {
			case 'tab.refresh':
				var tabContainnerId = action.id == undefined ? $.ajaxform.options.tabContainerId : action.id;
				var $tabContainner = $('#' + tabContainnerId);
				var $tab = action.args.title == undefined ? $tabContainner.tabs('getSelected') : $tabContainner.tabs('getTab', action.args.title);
				if (action.args.url == undefined) {
					$tab.panel('refresh');
				} else {
					$tab.panel('refresh', action.args.url);
				}
				break;
			case 'dialog.close':
				var dialogId = action.id == undefined ? $.dialog.defaults.id : action.id;
				$('#' + dialogId).dialog('close');
				break;
			case 'grid.reload':
				if (action.id == undefined) {
					var tabContainnerId = action.tabId == undefined ? $.ajaxform.options.tabContainerId : action.tabId;
					var $tabContainner = $('#' + tabContainnerId);
					var $tab = action.args.tabTitle == undefined ? $tabContainner.tabs('getSelected') : $tabContainner.tabs('getTab', action.args.tabTitle);
					$tab.find('.datagrid-f').each(function() {
						$.crudgrid.gridMethod($(this), 'reload');
					});
				} else {
					$.crudgrid.gridMethod($('#' + action.id), 'reload');
				}
				break;
			}
		},
		'callback' : function(data, options) {
			var result = $.parseJSON(data);
			var opts = $.extend({}, options, result);
			var message = opts.message;
			var title = opts.title;
			if (opts.success) {
				if (title == undefined) {
					title = "提示";
					if (message == undefined) {
						message = '操作成功';
					}
				} else {
					if (message == undefined) {
						message = title + '成功';
					}
				}
				$.messager.show({
					title : title,
					msg : message
				});
			} else {
				if (title == undefined) {
					title = "错误";
					if (message == undefined) {
						message = '操作失败';
					}
				} else {
					if (message == undefined) {
						message = title + '失败';
					}
				}
				$.messager.alert(title, opts.message, 'error');
			}
			// for ( var i in result.actions) {
			// $.ajaxform.callbackAction(result.actions[i]);
			// }
		},
		'submit' : function(options) {
			var $ajaxform = $.dialog.getForm(this);
			$ajaxform.form('submit', {
				ajax : true,
				iframe : false,
				onSubmit : function() {
					options.onBeforeSubmit.call(this, options);
					return $.ajaxform.validate.call(this);
				},
				success : function(data) {
					$.ajaxform.callback.call(this, data, options);
					options.onSubmitSuccess.call(this, data, options);
				}
			});
		}
	};

})(jQuery);
